//
//  XTCaseDetailsViewController.m
//  BeautifulHome
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 ACE. All rights reserved.
//

#import "XTCaseDetailsViewController.h"
#import "XTCaseViewController.h"
#import "UIImageView+WebCache.h"
@interface XTCaseDetailsViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic, retain)UIScrollView *scrollView;
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)UIView *myView;
@property(nonatomic, retain)NSMutableArray *photosArr;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)UIAlertView *alertView;
@end

@implementation XTCaseDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"案例详情";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    //收藏按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"shoucang1"] style:UIBarButtonItemStylePlain target:self action:@selector(shoucang)];
    
    
    
    //详情
    [self Xiangqing];
    
}
- (void)shoucang
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    //创建文件管理器(单例)
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //添加文件夹
    //拼接文件夹地址
    NSString *filePath = [path stringByAppendingPathComponent:@"收藏"];
    //创建文件夹
    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *strPath = [filePath stringByAppendingPathComponent:@"myCase.pp"];
    if (![fileManager fileExistsAtPath:strPath]) {
        NSMutableArray *arr = [NSMutableArray array];
        [arr addObject:self.xtCaseXiang];
        [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
        self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(dismissAlertView) userInfo:nil repeats:NO];
        [_alertView show];
    } else {
        NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:strPath];
        NSMutableArray *newArr = [NSMutableArray array];
        for (XTCase *xtCase in arr) {
            [newArr addObject: xtCase.title];
        }
        if (![newArr containsObject:self.xtCaseXiang.title]) {
            [arr addObject:self.xtCaseXiang];
            [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
            self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(dismissAlertView) userInfo:nil repeats:NO];
            [_alertView show];
        } else {
            self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(dismissAlertView) userInfo:nil repeats:NO];
            [_alertView show];
        }
    }
}
- (void)dismissAlertView
{
    [_alertView dismissWithClickedButtonIndex:0 animated:YES];
}

+ (CGFloat)heightWithText:(NSString *)text
{
    //设置一个字典保存文本属性
    NSDictionary *dic =[ NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
    //NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    
    //预设一个尺寸  文本最大不会超过这个尺寸
    CGSize size = CGSizeMake(375, 1000);
    //根据文本信息获得一个CGRect
    //参数1: 尺寸范围
    //参数2: 文本属性
    //参数3: 按照什么方式获取Rect
    //参数4: nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回得到的高度
    return rect.size.height;
}
- (void)Xiangqing
{
    CGFloat f = [[self class]heightWithText:_xtCaseXiang.title];
    CGFloat f1 = [[self class]heightWithText:_xtCaseXiang.mYdescription];
    
    
    //定义一个视图,将其放在collectionView上,文字放在视图上
    self.myView = [[UIView alloc]initWithFrame:CGRectMake(0, - (f + f1 +200), self.view.frame.size.width, f + f1 +200)];
    
    //头像
    UIImageView *imageViewHead = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 60, 80)];
    
    NSString *str = [[_xtCaseXiang.user objectForKey:@"userImage"]objectForKey:@"small"];
    [imageViewHead sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"my.jpg"]];
    
    [_myView addSubview:imageViewHead];
    [imageViewHead release];

    //标题
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, self.myView.frame.size.width - 90, f + 40)];
    //title.backgroundColor = [UIColor cyanColor];
    NSString *str7 = [NSString stringWithFormat:@"  %@",_xtCaseXiang.title];
    title.text = str7;
    title.numberOfLines = 0;
    [title setFont:[UIFont systemFontOfSize:20]];
    [_myView addSubview:title];
    [title release];
    //设计师
    
    UILabel *designer = [[UILabel alloc]initWithFrame:CGRectMake(100, f + 60, self.myView.frame.size.width - 100, 20)];
    //designer.backgroundColor = [UIColor redColor];
    NSString *str1 = [NSString stringWithFormat:@"设计师: %@",[self.user1 objectForKey:@"userName" ] ];
    designer.text = str1;
    [_myView addSubview:designer];
    [designer release];
    
    //风格
    UILabel *style = [[UILabel alloc]initWithFrame:CGRectMake(40, f + 90, 100, 20)];
    //style.backgroundColor = [UIColor greenColor];
    NSString *str2 = [NSString stringWithFormat:@"风格: %@", _xtCaseXiang.styleShow];
    style.text = str2;
    [_myView addSubview:style];
    //户型
    UILabel *houseStyle = [[UILabel alloc]initWithFrame:CGRectMake(220, f + 90, 100, 20)];
    //houseStyle.backgroundColor = [UIColor blueColor];
    NSString *str3 = [NSString stringWithFormat:@"户型: %@",_xtCaseXiang.typeShow];
    houseStyle.text = str3;
    [_myView addSubview:houseStyle];
    //面积
    UILabel *houseSize = [[UILabel alloc]initWithFrame:CGRectMake(40, f + 120, 150, 20)];
    //houseSize.backgroundColor = [UIColor orangeColor];
    NSString *str4 = [NSString stringWithFormat:@"面积: %@平米",_xtCaseXiang.areaShow];
    houseSize.text = str4;
    [_myView addSubview:houseSize];
    //预算
    UILabel *money = [[UILabel alloc]initWithFrame:CGRectMake(220, f + 120, 150, 20)];
    //money.backgroundColor = [UIColor cyanColor];
    NSString *str5 = [NSString stringWithFormat:@"预算: %@万",_xtCaseXiang.costShow];
    money.text = str5;
    [_myView addSubview:money];
    
    //案例简介
    UILabel *introduce = [[UILabel alloc]initWithFrame:CGRectMake(20, f + 140, self.view.frame.size.width - 40, 40)];
    //introduce.backgroundColor = [UIColor greenColor];
    introduce.text = @"案例简介";
    [introduce setFont:[UIFont systemFontOfSize:20]];
    [_myView addSubview:introduce];
    
    //描述
    UILabel *description1 = [[UILabel alloc]initWithFrame:CGRectMake(10, f + 180, self.view.frame.size.width - 20, f1 + 30)];
    //description1.backgroundColor = [UIColor cyanColor];
    NSString *str6 = [NSString stringWithFormat:@"    %@",self.desc];
    description1.text = str6;
    description1.numberOfLines = 0;
    //[description1 setTextAlignment:NSTextAlignmentLeft];
    [description1 setLineBreakMode:NSLineBreakByCharWrapping];
    [_myView addSubview:description1];
    
    
    
    //图片
    //1.设置collectionView布局信息
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //layout设置
    //item 最小行间距
    flowLayout.minimumLineSpacing = 10;
    //2.创建collectionView
    //2.1创建对象
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 70) collectionViewLayout:flowLayout];
    //2.2设置代理
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    //2.3添加父视图
    
    [self.collectionView addSubview:_myView];
    [self.view addSubview:_collectionView];
    //2.4内存管理
    [_collectionView release];
    [flowLayout release];

    [_collectionView registerClass:[UICollectionViewCell
                                    class] forCellWithReuseIdentifier:@"cell"];
    
    self.photosArr = _xtCaseXiang.photos;
   // NSLog(@"$$$$%@",_photosArr);
    
    
    
    //collectionView上方放置基本信息
    _collectionView.contentInset = UIEdgeInsetsMake(f + f1 + 200, 0, 0, 0);


    
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _photosArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    

    UICollectionViewCell *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    //图片
    UIImageView *imageViewHead = [[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, self.collectionView.frame.size.width - 20, 200)]autorelease];
    id photoId = [[_photosArr objectAtIndex:indexPath.row]objectForKey:@"id"];
    NSString *string = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w640_h0_m0.jpg", photoId];
    [imageViewHead sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"3-1"]];
    
    //图片描述
    CGFloat f2 = [[self class]heightWithText:[[_photosArr objectAtIndex:indexPath.row]objectForKey:@"comment"]];
    
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 210, self.view.frame.size.width - 20, f2 + 30 )] autorelease];
    label.text = [[_photosArr objectAtIndex:indexPath.row]objectForKey:@"comment"];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    label.numberOfLines = 0;
    
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    
    [cell.contentView addSubview:imageViewHead];
    [cell.contentView addSubview:label];
    return cell;
    
   
    
    
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    XTPictureViewController *pVC = [[[XTPictureViewController alloc]init]autorelease];
    pVC.pictureArr = _xtCaseXiang.photos;
    pVC.page = indexPath.row;
    
    
    
    [self.navigationController pushViewController:pVC animated:YES];
}



//item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat f2 = [[self class]heightWithText:[[_photosArr objectAtIndex:indexPath.row]objectForKey:@"comment"]];
    return CGSizeMake(self.view.frame.size.width, 220 + f2);
}

//Item的边界大小
//上左下右
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 49, 5);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
